package me.lc.linklink.scene;

import me.lc.linklink.MainActivity;
import me.lc.linklink.core.GameSystem;
import me.lc.linklink.core.MyWiGameAllClient;
import me.lc.linklink.layer.HelpLayer;
import me.lc.linklink.layer.MainMenuBGLayer;
import me.lc.linklink.layer.MainMenuLayer;
import me.lc.linklink.layer.StageMenuLayer;
import android.view.KeyEvent;

import com.wiyun.engine.nodes.Scene;
import com.wiyun.game.WiGame;
import com.wiyun.game.model.ChallengeRequest;

public class MainScene extends Scene {
	MainMenuBGLayer bgLayer;
	MainMenuLayer menuLayer;
	StageMenuLayer stageMenuLayer;
	HelpLayer helpLayer;

	private boolean isInStageChoose = false;
	private boolean isInHelp = false;

	private static MainScene instance = null;
	private static Object sync_obj = new Object();

	protected MainScene() {
		bgLayer = new MainMenuBGLayer();
		addChild(bgLayer);

		menuLayer = new MainMenuLayer();
		addChild(menuLayer);

		stageMenuLayer = new StageMenuLayer();
		stageMenuLayer.setVisible(false);
		addChild(stageMenuLayer);

		helpLayer = new HelpLayer();
		helpLayer.setVisible(false);
		addChild(helpLayer);

		Thread t = new Thread(new Runnable() {
			@Override
			public void run() {
				// 初始化wigame
				MainActivity.instance.initWiGame();
			}
		});
		t.start();

	}

	public void showStageMenu() {
		// TODO lc 此处可以做动画
		isInStageChoose = true;
		stageMenuLayer.setVisible(true);
		menuLayer.setVisible(false);
		stageMenuLayer.refreshBtns();
	}

	public void showHelpLayer() {
		isInHelp = true;
		helpLayer.setVisible(true);
		menuLayer.setVisible(false);
	}

	public void showMainMenu() {
		isInStageChoose = false;
		isInHelp = false;
		stageMenuLayer.setVisible(false);
		helpLayer.setVisible(false);
		menuLayer.setVisible(true);
	}

	@Override
	public boolean wyKeyDown(KeyEvent event) {

		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			if (isInStageChoose || isInHelp) {
				showMainMenu();
			} else {
				MainActivity.instance.showExitTips();
			}
			return true;
		}
		return super.wyKeyDown(event);
	}

	public static MainScene make() {
		synchronized (sync_obj) {
			if (instance == null) {
				instance = new MainScene();
				// instance.autoRelease(true);
			}
			return instance;
		}

	}
}
